package com.tourlaid.mamagerment.ao.wx.impl

import com.tourlaid.mamagerment.ao.wx.MyThoughtDetailAo
import com.tourlaid.mamagerment.entity.User
import com.tourlaid.mamagerment.service.CommentService
import com.tourlaid.mamagerment.service.RewardService
import com.tourlaid.mamagerment.service.UserService
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/8/11.
 */
@Service("myThoughtDetailAo")
class MyThoughtDetailAoImpl : MyThoughtDetailAo {

    @Resource
    val commentService: CommentService? = null
    @Resource
    val rewardService: RewardService? = null
    @Resource
    val userService: UserService? = null

    override fun queryComments(thoughtId: Long): MutableList<MutableMap<String, Any?>> {
        var list: MutableList<MutableMap<String, Any?>> = mutableListOf()
        val commentList = commentService!!.queryByThoughtId(thoughtId);
        if (CollectionUtils.isEmpty(commentList)) return list
        for (comment in commentList) {
            var map = mutableMapOf(
                    "id" to comment.id,
                    "fromUserId" to comment.fromUserId,
                    "toUser" to comment.toUserId,
                    "content" to comment.content
            )
            list.add(map)
        }
        return list
    }

    override fun queryRewardRecords(thoughtId: Long): MutableList<MutableMap<String, Any?>> {
        var list: MutableList<MutableMap<String, Any?>> = mutableListOf()
        val rewardList = rewardService!!.queryByThoughtId(thoughtId)
        if (CollectionUtils.isEmpty(rewardList)) {
            return list
        }
        val userIdList = rewardList.map { reward -> reward.userId!! }
        var userList = userService!!.queryByIdList(userIdList)
        for (reward in rewardList) {
            var userT = User()
            for (user in userList) {
                if (Objects.equals(reward.userId, user.id)) {
                    userT = user
                    break;
                }
            }
            var map = mutableMapOf(
                    "picture" to userT.picture,
                    "nickName" to userT.nickName,
                    "integral" to reward.integral
            )
            list.add(map)
        }
        return list;
    }

}